package com.llm.travel_agent_assistant.controller;

import com.llm.travel_agent_assistant.dto.Result;
import com.llm.travel_agent_assistant.dto.TravelPlanDTO;
import com.llm.travel_agent_assistant.dto.UserDTO;
import com.llm.travel_agent_assistant.entity.User;
import com.llm.travel_agent_assistant.service.TravelPlanService;
import com.llm.travel_agent_assistant.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private TravelPlanService travelPlanService;
    
    @Resource
    private HttpServletRequest request;

    @PostMapping("/registry")
    public Result registry(@RequestBody User user) {
        return userService.register(user);
    }

    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO) {
        return userService.login(userDTO);
    }

    @GetMapping("/query_travel_plans")
    public Result queryTravelPlans(@RequestParam("userId") String userId) {
        return travelPlanService.getPlanByUserId(userId);
    }

    @PostMapping("/update_travel_plan")
    public Result updateTravelPlan(@RequestBody TravelPlanDTO travelPlanDTO) {
        return travelPlanService.updatePlan(travelPlanDTO);
    }

    @GetMapping("/get_travel_plan_detail")
    public Result getTravelPlanDetail(@RequestParam("planId") String planId) {
        return travelPlanService.getPlanDetail(planId);
    }

    @PostMapping("/logout")
    public Result logout() {
        // 从session中获取userId
        HttpSession session = request.getSession(false);
        if (session == null) {
            return Result.fail("用户未登录");
        }
        
        String userId = (String) session.getAttribute("userId");
        if (userId == null) {
            return Result.fail("用户未登录");
        }
        
        return userService.logout(userId);
    }
}
